<template>
<table>
    <tr>
        <td>理财项目名称</td>
        <td>{{ info.F_Name}}</td>
    </tr>
    <tr>
        <td>年收益</td>
        <td>{{ info.F_ARY
 }}</td>
    </tr>
    <tr>
        <td>最低金额</td>
        <td>{{ info.F_MinMoney }}</td>
    </tr>
    <tr>
        <td>风险等级</td>
        <td>{{ Level }}</td>
    </tr>
    <tr>
        <td>购买金额</td>
        <td><input type="text" @change="MoneyChange" v-model="data.UF_Money"></td>
    </tr>
    <tr>
        <td>每年预计收益</td>
        <td>{{ LowMoney }}CNY</td>
    </tr>
    <tr>
        <td colspan="2"><button @click="AddUserFinan">保存</button></td>
    </tr>
</table>
</template>
<script setup lang="ts">
import {useRoute,useRouter} from 'vue-router'
import axios from 'axios'
import {onMounted,ref} from 'vue'
import { useUserStore } from '../../Store';
const userStore = useUserStore();
const route=useRoute()
const router=useRouter()
const data=ref(
    {
        UF_Money:"",
        F_Id:route.query.F_Id,
        U_Id:userStore.Uid
    }
)
const LowMoney=ref(0)
const MoneyChange=()=>{
     LowMoney.value=Number(data.value.UF_Money)*Number(info.value.F_ARY)/100
     console.log(data.value.UF_Money)
}
var Fid=route.query.F_Id
onMounted(()=>{
    SelectById()
})
const info:any=ref({});
var Level=ref("")
const SelectById=()=>{
    axios.get('https://localhost:44358/api/Finan/SelectById',
        {
            params:{
                Fid:Fid
            }
        }
    ).then(res=>{
        info.value=res.data
        if(res.data.F_Level==1){
            Level.value="低风险"
        }else if(res.data.F_Level==2){
            Level.value="中风险"
        }else{
            Level.value="高风险"
        }
        console.log(res.data)
    }).catch(err=>{
        console.log(err);
        }
    )
}
const AddUserFinan=()=>{
    if(data.value.UF_Money<info.value.F_MinMoney){
        alert("金额不能小于最低金额")
        return
    }
    console.log(data.value)
    console.log(info.value
        
    )

    axios.post("https://localhost:44358/api/Finan/AddUserFinan" ,
        {

            UF_Money:data.value.UF_Money,
            F_Id:data.value.F_Id,
            U_Id:data.value.U_Id
        }
    ).then(res=>{
        if(res.data>0){
            alert("投资成功")
            location.href="/ShowUserFinan"
        }else{
            alert("投资失败")
        }
    }).catch(err=>{
        console.log(err);
        }
    )
}

</script>